home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.08 Aug 93 / Scripting Additions / CoercescptToText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  2.3 KB  |  93 lines  |  [TEXT/MPS ]

  1. ////////////////////////////////////////////////////////////////////
  2. //
  3. //      CoercescptToText.c written by Donald O. Olson
  4. //      A simple Coercion Scripting Addition written
  5. //        to illustrate writing Scripting Additions.
  6. //
  7. //         Copyright ®1993 Donald O. Olson
  8. //        All rights reserved.
  9. //
  10. //////////////////////////////////////////////////////////////////
  11.  
  12. #include <Memory.h>
  13. #include <Fonts.h>
  14. #include <OSEvents.h>
  15. #include <Menus.h>
  16. #include <Processes.h>
  17. #include <String.h>
  18. #include <Resources.h>
  19. #include <Packages.h>
  20. #include <AppleEvents.h>
  21. #include <Errors.h>
  22. #include <GestaltEqu.h>
  23. #include <Files.h>   
  24. #include <OSA.h>   
  25.  
  26. #define typeStyledText     'STXT'
  27.  
  28. //////////////////////////////////////////////////////////////////
  29. //
  30. //     main()
  31. //    This is the interface for a coerce from descriptor
  32. //    coercion. Remember to declare it 'pascal'!!!
  33. //
  34. //////////////////////////////////////////////////////////////////
  35.  
  36. pascal OSErr main(    AEDesc *scriptDesc, DescType toType, 
  37.                 long refcon, AEDesc *resultDesc)
  38. {
  39.     OSErr                 theErr = noErr;
  40.     OSAError            theOSAErr;
  41.     ComponentInstance    gASComponent = 0;
  42.     OSAID                resultingID = 0;
  43.     long                modeFlags = 0;
  44.  
  45.     /* 
  46.         Open an instantiation of the 
  47.         Generic Scripting Component 
  48.     */
  49.     
  50.     gASComponent = OpenDefaultComponent(kOSAComponentType,                     
  51.                     kOSAGenericScriptingComponentSubtype);
  52.     
  53.     /* Checking errors here! */
  54.     if(((long)gASComponent == (long)badComponentInstance) || 
  55.         ((long)gASComponent == (long)badComponentSelector)) {
  56.         theErr = invalidComponentID; 
  57.         goto CLEANUP;    // Yea! A valid use for a 'goto'!!
  58.     }
  59.     
  60.     /* Load script in scriptDesc into a scriptID */
  61.     theOSAErr = OSALoad(    gASComponent,
  62.                             scriptDesc,
  63.                             modeFlags,
  64.                             &resultingID);
  65.     
  66.     
  67.     if(    theOSAErr != noErr) {
  68.         theErr = theOSAErr;
  69.         goto CLEANUP;
  70.     }
  71.     
  72.     /*     
  73.         Now get the source. Since AppleScript can coerce any
  74.         of the various text forms to a text object (which is
  75.         what we claim to be returning, let's just return the
  76.         styled text and let AppleScript do the secondary         
  77.         coercion for us.
  78.     */
  79.     
  80.     theOSAErr = OSAGetSource(    gASComponent,
  81.                         resultingID,
  82.                         typeStyledText,
  83.                          resultDesc);
  84.                              
  85.     if(    theOSAErr != noErr) theErr = theOSAErr;
  86.     
  87.     CLEANUP:;    
  88.     if(resultingID != 0) OSADispose(gASComponent, resultingID);
  89.     if(gASComponent != 0) CloseComponent(gASComponent);
  90.  
  91.     return theErr;
  92. }
  93.